package com.ruoyi.project.oms.paeaAuBatch.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.oms.paeaAuAuditItem.domain.PaeaAuAuditItem;
import com.ruoyi.project.oms.paeaAuAuditItem.enums.AuditItemState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.oms.paeaAuBatch.mapper.PaeaAuBatchMapper;
import com.ruoyi.project.oms.paeaAuBatch.domain.PaeaAuBatch;
import com.ruoyi.project.oms.paeaAuBatch.service.IPaeaAuBatchService;
import com.ruoyi.common.utils.text.Convert;
/**
 * 监督批次Service业务层处理
 *
 * @author ruoyi
 * @date 2020-03-24
 */
@Service
public class PaeaAuBatchServiceImpl extends ServiceImpl<PaeaAuBatchMapper, PaeaAuBatch> implements IPaeaAuBatchService
{
    @Autowired
    private PaeaAuBatchMapper paeaAuBatchMapper;

    /**
     * 查询监督批次
     *
     * @param id 监督批次ID
     * @return 监督批次
     */
    @Override
    public PaeaAuBatch selectPaeaAuBatchById(String id)
    {
        return paeaAuBatchMapper.selectPaeaAuBatchById(id);
    }

    /**
     * 查询监督批次列表
     *
     * @param paeaAuBatch 监督批次
     * @return 监督批次
     */
    @Override
    public List<PaeaAuBatch> selectPaeaAuBatchList(PaeaAuBatch paeaAuBatch)
    {
        return paeaAuBatchMapper.selectPaeaAuBatchList(paeaAuBatch);
    }

    /**
     * 新增监督批次
     *
     * @param paeaAuBatch 监督批次
     * @return 结果
     */
    @Override
    public int insertPaeaAuBatch(PaeaAuBatch paeaAuBatch)
    {
        return paeaAuBatchMapper.insertPaeaAuBatch(paeaAuBatch);
    }

    /**
     * 修改监督批次
     *
     * @param paeaAuBatch 监督批次
     * @return 结果
     */
    @Override
    public int updatePaeaAuBatch(PaeaAuBatch paeaAuBatch)
    {
        return paeaAuBatchMapper.updatePaeaAuBatch(paeaAuBatch);
    }

    /**
     * 删除监督批次对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaeaAuBatchByIds(String ids)
    {
        return paeaAuBatchMapper.deletePaeaAuBatchByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除监督批次信息
     *
     * @param id 监督批次ID
     * @return 结果
     */
    @Override
    public int deletePaeaAuBatchById(String id)
    {
        return paeaAuBatchMapper.deletePaeaAuBatchById(id);
    }

    /**
     * 查询监督批次
     * @param orgId
     * @param batchType
     * @param beginDate
     * @param endDate
     * @param status
     * @param manner
     * @param queryFlag
     * @param userId
     * @return 监督批次
     */
    @Override
    public List<PaeaAuBatch> queryPaeaAuBatchs(String orgId, int batchType, java.util.Date beginDate,
                                               java.util.Date endDate, int status, String manner, String queryFlag, String userId){
        return paeaAuBatchMapper.queryPaeaAuBatchs(orgId, batchType, beginDate, endDate, status, manner, queryFlag, userId);
    }

    /**
     * 质量检查查询批次
     * @param orgId
     * @param batchType
     * @param state
     * @param beginDate
     * @param endDate
     * @param userId
     * @return 监督批次
     */
    @Override
    public List<PaeaAuBatch> queryReAuditBatchList(String orgId, int batchType, int state, java.util.Date beginDate,
                                                   java.util.Date endDate, String userId, String auditorId){
        return paeaAuBatchMapper.queryReAuditBatchList(orgId, batchType, state, beginDate, endDate, userId, auditorId);
    }

    /**
     * 通过id查找值返回组合字符串
     * @param auBatchId
     * @param titleContext
     * @param isByRandom
     * @param items
     * @return
     */
    @Override
    public String obtainReturnString(String auBatchId, String titleContext, boolean isByRandom,List<PaeaAuAuditItem> items) {
        int countY = 0;
        int countE = 0;
        int countN = 0;
        int countRad = 0;
        for (int i = 0; i < items.size(); i++) {
            PaeaAuAuditItem item = items.get(i);
            if ("9999".equals(item.getAuTypeId())) {
                countRad++;// 随机
            }
            if (item.getState() == AuditItemState.UN_AUDIT.getCode()) {
                countN++;// 未监控
            }
            if (item.getState() == AuditItemState.NORMAL_PASS.getCode()) {
                countY++;// 正常
            }
            if (item.getState() == AuditItemState.HAVE_ERROR.getCode()) {
                countE++;// 有风险
            }
        }
        String split1 = "$$$";// 数量分隔
        String split2 = "###";// 数量与实体分隔
        String split3 = "@@@";// 实体记录元素之间分隔
        String split4 = "&&&";// 记录之间分隔
        // 表头统计信息
        StringBuffer returnString = new StringBuffer(countN + split1 + countY
                + split1 + countE + split1 + (countY + countN + countE)
                + split1 + (countY + countN + countE - countRad) + split1
                + countRad + split1 + (isByRandom ? "1" : "0"));
        // 表头名称
        returnString.append(split2 + titleContext);
        // 批次id
        returnString.append(split2 + auBatchId);
        return returnString.toString();
    }
}
